package com.hwadee.property.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hwadee.property.pojo.Notice;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface NoticeMapper extends BaseMapper<Notice> {

    // 计算总记录数（如果 title 为空，不使用 LIKE；同时过滤 propertyStaffId）
    @Select("<script>" +
            "SELECT COUNT(*) FROM notice " +
            "<where>" +
            "   property_staff_id = #{propertyStaffId} " +
            "   <if test='title != null and title.trim() != \"\"'> " +
            "       AND title LIKE CONCAT('%', #{title}, '%') " +
            "   </if> " +
            "</where>" +
            "</script>")
    int countNotices(@Param("propertyStaffId") Long propertyStaffId, @Param("title") String title);

    // 分页查询（如果 title 为空，不使用 LIKE；同时过滤 propertyStaffId）
    @Select("<script>" +
            "SELECT id, title, attachment, remark, property_staff_id, publish_date, update_date " +
            "FROM notice " +
            "<where>" +
            "   property_staff_id = #{propertyStaffId} " +
            "   <if test='title != null and title.trim() != \"\"'> " +
            "       AND title LIKE CONCAT('%', #{title}, '%') " +
            "   </if> " +
            "</where> " +
            "ORDER BY publish_date ASC " +  // 按发布时间倒序排列
            "LIMIT #{size} OFFSET #{offset}" +
            "</script>")
    List<Notice> getNotices(@Param("propertyStaffId") Long propertyStaffId, @Param("title") String title, @Param("size") int size, @Param("offset") int offset);

    // 总数（支持多个物业人员ID）
    @Select("<script>" +
            "SELECT COUNT(*) FROM notice " +
            "<where>" +
            "property_staff_id IN " +
            "<foreach item='id' collection='staffIds' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "<if test='title != null and title.trim() != \"\"'> " +
            "AND title LIKE CONCAT('%', #{title}, '%') " +
            "</if>" +
            "</where>" +
            "</script>")
    int countNoticesByStaffIds(@Param("staffIds") List<Long> staffIds, @Param("title") String title);

    // 分页（支持多个物业人员ID）
    @Select("<script>" +
            "SELECT id, title, attachment, remark, property_staff_id, publish_date, update_date " +
            "FROM notice " +
            "<where>" +
            "property_staff_id IN " +
            "<foreach item='id' collection='staffIds' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "<if test='title != null and title.trim() != \"\"'> " +
            "AND title LIKE CONCAT('%', #{title}, '%') " +
            "</if>" +
            "</where> " +
            "ORDER BY publish_date DESC " +
            "LIMIT #{size} OFFSET #{offset}" +
            "</script>")
    List<Notice> getNoticesByStaffIds(@Param("staffIds") List<Long> staffIds,
                                      @Param("title") String title,
                                      @Param("size") int size,
                                      @Param("offset") int offset);

    // 统计总记录数（不传 propertyStaffId，只根据 title 查询）
    @Select("<script>" +
            "SELECT COUNT(*) FROM notice " +
            "<where>" +
            "   <if test='title != null and title.trim() != \"\"'> " +
            "       title LIKE CONCAT('%', #{title}, '%') " +
            "   </if> " +
            "</where>" +
            "</script>")
    int countAllNotices(@Param("title") String title);

    // 分页查询（不传 propertyStaffId，只根据 title 查询）
    @Select("<script>" +
            "SELECT id, title, attachment, remark, property_staff_id, publish_date, update_date " +
            "FROM notice " +
            "<where>" +
            "   <if test='title != null and title.trim() != \"\"'> " +
            "       title LIKE CONCAT('%', #{title}, '%') " +
            "   </if> " +
            "</where> " +
            "ORDER BY publish_date ASC " +
            "LIMIT #{size} OFFSET #{offset}" +
            "</script>")
    List<Notice> getAllNotices(@Param("title") String title, @Param("size") int size, @Param("offset") int offset);

}
